package com.javaprint.order.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ReflectionUtils;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/*
 *解析对象属性值
 * @param null
 * @author LiQiuLin
 * @date 2020/3/18
 */
public class FieldReflectUtil<T> {
    private static final Logger logger = LoggerFactory.getLogger(FieldReflectUtil.class);
    private T data;

    public FieldReflectUtil(T data) {
        this.data = data;
    }
    public Map<String,Object> getFieldValue() {
        Map<String,Object> map = new HashMap<>();
        Class<?> clazz = data.getClass();
        Field[] selfFields = clazz.getDeclaredFields();
        Class<?> superClazz = clazz.getSuperclass();
        if(superClazz.equals(Object.class)){
            logger.info("没有父类");
            for (int i = 0; i < selfFields.length; i++) {
                PropertyDescriptor pd = null;
                try {
                    pd = new PropertyDescriptor(selfFields[i].getName(), clazz);
                }catch (IntrospectionException p){
                    logger.error("获取属性解析异常",p);
                    continue;
                }
                Method getMethod = pd.getReadMethod();//获得get方法
                Object fieldValue = ReflectionUtils.invokeMethod(getMethod, data);
                if(fieldValue == null){
                    continue;
                }
                logger.info("字段{}的值为：{}",selfFields[i].getName(),fieldValue.toString());
                map.put(selfFields[i].getName(),fieldValue);
            }
        }else {
            //有父类，获取父类和本类的值
            Field[] tableSuperFields = superClazz.getDeclaredFields();
            Field[] superFields = new Field[selfFields.length + tableSuperFields.length];
            System.arraycopy(selfFields, 0, superFields, 0, selfFields.length);
            System.arraycopy(tableSuperFields, 0, superFields, selfFields.length, tableSuperFields.length);
            Field[] allFields = getSuperClassFields(superFields, superClazz);
            for (int i = 0; i < allFields.length; i++) {
                PropertyDescriptor pd = null;
                try {
                    pd = new PropertyDescriptor(allFields[i].getName(), clazz);
                }catch (IntrospectionException p){
                    logger.error("检查异常!",p);
                }
                Method getMethod = pd.getReadMethod();//获得get方法
                Object fieldValue = ReflectionUtils.invokeMethod(getMethod, data);
                if(fieldValue == null){
                    continue;
                }

                logger.info("字段{}的值为：{}",allFields[i].getName(),fieldValue.toString());
                map.put(selfFields[i].getName(),fieldValue);
            }
        }
        return map;
    }
    //获取父类的所有字段
    private static Field[] getSuperClassFields(Field[] tableFields, Class<?> clazz) {
        Class<?> superClazz = clazz.getSuperclass();
        if (superClazz.equals(Object.class)) {
            return tableFields;
        }
        Field[] tableSuperFields = superClazz.getDeclaredFields();

        Field[] c = new Field[tableFields.length + tableSuperFields.length];
        System.arraycopy(tableFields, 0, c, 0, tableFields.length);
        System.arraycopy(tableSuperFields, 0, c, tableFields.length, tableSuperFields.length);
        getSuperClassFields(c, superClazz);
        return c;
    }
}
